'''
Created on April 9, 2011

@author: yc30v1m
'''
from logging import getLogger
from types import TypeType
from library.executors.TestCase import TestCase
from testscripts.CountVerification import CountVerification #@UnusedImport
from testscripts.MultipleRows import MultipleRows #@UnusedImport
from testscripts.ThreadedMultipleRows import ThreadedMultipleRows #@UnusedImport
from testscripts.CompositeKeyValidation import CompositeKeyValidation #@UnusedImport
import os
from library.AutoLoad import AutoLoad
import string

log = getLogger(__name__)

class Factory(object):
    '''
    classdocs
    '''
    _testCases = {}
    __fileFQD='plugins'
    
    _executionTypeValidStrings = ['TYPE', 'EXEC_TYPE', 'EXECTYPE', 'Type', 'Execution Type']
    
    def __init__(self, testCases):
        self._testCases = testCases
        self.executeTestCase()
        
    def executeTestCase(self):
        try:
            TestCaseClasses = [j for (i,j) in globals().iteritems() if isinstance(j, TypeType) and issubclass(j, TestCase)] #@UnusedVariable
            for testCase in self._testCases:
                testCaseExecutionType = self.__identifyExecutionType(self._testCases[testCase])
                for TestCaseClass in TestCaseClasses:
                    if TestCaseClass.supportedExecutionTypes(testCaseExecutionType):
                        
                        TestCaseClass(self._testCases[testCase])
        except:
            log.warning( 'No TestCase containing "%s".' % testCaseExecutionType)
 
    def __identifyExecutionType(self, testCase):
        executionType = None
        for type in self._executionTypeValidStrings:
            try:
                executionType = testCase[type]
                if executionType == None:
                    continue
                else:
                    return executionType
            except:
                executionType = None
                None 
    
    def __importAlltestScriptFiles(self):
        scriptsPath = 'testscripts'
        files = os.listdir(scriptsPath)
        importableFiles = {}
        for file in files:
            prefix, ext = file.split(".")
            if prefix != '__init__':
                importableFiles[prefix] = prefix
        for k in importableFiles:
            AutoLoad.ClassOnly(scriptsPath + '.' + k)
   


        